<?
//ob
ob_start();

//configuration
include "../conf.php";

//includes
include "../php/functions.php";

include "../php/facebook.api.php";
include "../php/auth.facebook.php";


//connect2db
$_DB = new PDO("mysql:host={$_MYSQL['host']};dbname={$_MYSQL['db_name']}", $_MYSQL['db_user'], $_MYSQL['password']);

if($_REQUEST['query']) {
	switch($_REQUEST['source']) {
		case 'sessions':
			$data=array_map('strtotime',$_DB->query('SELECT `play_time` FROM `song_sessions`')->fetchAll(PDO::FETCH_COLUMN));
			$labelname="sessions";
			break;
			
		case 'songs':
			$data=array_map('strtotime',$_DB->query('SELECT `s.title` FROM `song_sessions` ss join `songs` s on ss.`song_id`=s.`song_id`')->fetchAll(PDO::FETCH_COLUMN));
			$labelname="songs";
			break;
			
		case 'users':
			$data=array_map('strtotime',$_DB->query('SELECT `regdate_first` FROM `user`')->fetchAll(PDO::FETCH_COLUMN));
			$labelname="users";
			break;

		default: exit;
	}
	switch($_REQUEST['format']) {
		case 'hour':
			$datestring='Y-m-d H\h';
			$datediff='-1 hour';
			break;
		case 'day':
			$datestring='Y-m-d';
			$datediff='-1 day';
			break;
		default: exit;
	}
	if(!$_REQUEST['range']) exit;
	//gen dataarray
	$time=time();
	for($i=0;$i<intval($_REQUEST['range']);$i++) {
		$dataset[date($datestring,$time)]=0;
		$time=strtotime($datediff,$time);
	}
	$dataset=array_reverse($dataset);
	foreach($data as $dat) {
		if(isset($dataset[date($datestring,$dat)]))
			$dataset[date($datestring,$dat)]++;
	}
	
	//gen result
	$result['label']=array($labelname);
	foreach($dataset as $name => $value) {
		$result['values'][]=array('label'=>$name,'values'=>array($value));
	}	
	echo json_encode($result, JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP);

exit;
}

?>
<script type="text/javascript" src="../js/mootools-core.js"></script>
<script type="text/javascript" src="charts.js"></script>
<script type="text/javascript">
window.addEvent("domready",function(){
	areaChart = new $jit.BarChart({  
	  //id of the visualization container  
	  injectInto: 'infovis',  
	  //add animations  
	  animate: true,  
	  orientation: 'vertical',  
	  //separation offsets  
	  Margin: {  
		top: 5,  
		left: 5,  
		right: 5,  
		bottom: 5  
	  },  
	  labelOffset: 10,  
	  //whether to display sums  
	  showAggregates: true,  
	  //whether to display labels at all  
	  showLabels: true,  
	  //could also be 'stacked'  
	  type: true? 'stacked:gradient' : 'stacked',  
	  //label styling  
	  Label: {  
		type: 'Native', //can be 'Native' or 'HTML'  
		size: 13,  
		family: 'Arial',  
		color: 'black'  
	  },  
	  //enable tips  
	  Tips: {  
		enable: true,  
		onShow: function(tip, elem) {  
		  tip.innerHTML = "<b>" + elem.name + "</b>: " + elem.value;  
		}  
	  },  
	  //add left and right click handlers  
	  filterOnClick: true,  
	  restoreOnRightClick:true  
	});  
	//load JSON data.  
	http://fbapp.rene-pickhardt.de/admin/charts?query=query&source=users&format=day&range=5
	
	updateData();
	
	
	$("updatebutton").addEvent('click',updateData);
});

function updateData() {
	new Request.JSON({
		onSuccess: function(json) {
				console.info(json);
				areaChart.loadJSON(json);
			}
	}).post({query:'query',source:$('source').get('value'),format:$('format').get('value'),range:$('range').get('value')});
}


</script>
<select id=source>
      <option value=sessions>sessions</option>
      <option value=users>users</option>
      <option value=songs>songs</option>
</select>
<select id=format>
      <option value=day>days</option>
      <option value=hour>hours</option>
</select>
range: <input value=10 id=range>
<input type=submit value="Update!" id=updatebutton>
<div id="infovis" style="height:400px;background:white"></div>